define("carts",["jquery","cookie"],function ($) {
    $.ajax({
        url:"./server/getlist.php",
    }).done(function (res) {
       res.forEach(function (ele,index) {
           console.log(ele.c_size[1]);
           console.log(ele, index);
           var html=`  
                    <div class="dright" data-info='${JSON.stringify(ele)}'>
                    <ul>
                        <li class="first">
                            <dl>
                                <dd><a href="#">Marcelo Burlon</a></dd>
                                <dd><a href="#" name="cname">${ele.c_name}</a></dd>
                                <dd>货号: 5Lux1408434g_p4840790</dd>
                            </dl>
                        </li>
                        <li class="second">
                            <span>￥<i style="font-style: normal" class="price">${ele.c_price}</i></span>
                            <span>￥ 20017</span>
                            <div class="red-card">
                                <span>红卡会员价</span>
                            </div>
                            <div class="qcode">
                                <img src="img/fcode-1.jpg" alt="">
                                <i></i>
                                <div class="text">扫码下载手机客户端</div>
                            </div>
                        </li>
                        <li class="third">
                            <div class="fenqi">
                                <span>分期价</span>
                                <span>¥1195.49</span>
                                <div class="date">× 12期 ></div>
                            </div>
                            <div class="server">
                                <img src="img/server.png" alt="">
                            </div>
                        </li>
                        <li class="four">
                            <span>税费：</span>
                            <span>税费信息 </span>
                            <i></i>
                        </li>
                        <li class="five">
                            <span>大小</span>
                            <div class="ITEMS">
                                <div class="item current"><a href="javascript:void(0)">${ele.c_size[0]}</a></div>
                                <div class="item"><a href="javascript:void(0)">${ele.c_size[1]}</a></div>
                                <div class="item"><a href="javascript:void(0)">${ele.c_size[2]}</a></div>
                                <div class="item"><a href="javascript:void(0)">${ele.c_size[3]}</a></div>
                            </div>
                        </li>
                        <li class="six">
                            <span>颜色</span>
                            <div class="color">
                                <div class="it">Block</div>
                            </div>
                        </li>
                        <li class="seven">
                            <span>数量：</span>
                            <div class="num">
                                <a  href="javascript:void(0)" class="del" >-</a>
                                <input type="text" value="${ele.c_num}" class="res">
                                <a href="javascript:void(0)" class="add">+</a>
                            </div>
                        </li>
                        <li class="eight">
                            <div class="buy">
                                <a href="javascript:void(0)"><span>立即购买</span></a>
                            </div>
                            <div class="car"><a class="join" href="javascript:void(0)" data-info=${ele.c_id}><span>加入购物袋</span><i></i></a></div>
            
                        </li>
                        <li></li>
                    </ul>
                </div>`
           ele.c_size.forEach(function (ele,index) {
               console.log(ele,index);
               $(this).onclick=function () {
                   console.log(ele);
               }

           })
                       $(".detail").append(html)
                   })
    })
    $(document).on("click",".join",function () {
        var pid=$(this).data("info")
        var Info=$(this).parents(".dright").data("info")
        if(sessionStorage.getItem("userInfo")==null){
            //没有登录 把数据保存到cookie中
            var cookielist=JSON.parse($.cookie("carts")||'[]')
            //遍历
            var status=false
            cookielist.forEach((ele,index)=>{
                //如果有 就数量加1
                if(ele.c_id==pid){
                    status=true
                  ele.c_num += 1;
                }
            })
            if(status==false){
                Info.c_num=1
                cookielist.push(Info)
            }
            $.cookie("carts",JSON.stringify(cookielist))
        }else{
            //登录了 就把数据(用户id 和 产品信息)保存到数据库中
            var formData=new FormData();
            var useinfo=JSON.parse(sessionStorage.getItem("userInfo"))
            formData.append("u_id",useinfo.data.u_id)
            for(var ele in Info){
                console.log(ele);
                formData.append(ele,Info[ele])
            }
            $.ajax({
                type:"post",
                url:"http://127.0.0.1/diwudadao/server/insertcarts.php",
                data:formData,
                dataType:"json",
                processData:false,
                contentType:false
            }).done(function (res) {
                console.log(res);
            })

        }
    })
    $(document).on("click",".quite",function () {
            $.get("./server/quite.php",function () {
                alert("退出成功")
                window.location.reload(true)
            })
    })
})